package com.wisemoresystem.seal.action.seal.set;

import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_seal;
import com.wisemoresystem.seal.model.t_seal_new;
import com.wisemoresystem.seal.model.t_seal_type;
import com.wisemoresystem.seal.model.t_seal_type_config;
import com.wisemoresystem.seal.service.SealNewService;
import com.wisemoresystem.seal.service.SealSetService;
import com.wisemoresystem.seal.service.SealTypeConfigService;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 印章停用
 */
@Controller("ixSealSet")
@Scope("prototype")
@RequestMapping("/ix/seal/set")
public class SealSetAction extends ProjBaseAction {

	@Autowired
	private SealSetService sealService;
	@Autowired
	private SealTypeConfigService sealTypeService;
	@Autowired
	private SealNewService sealNewService;

	/**
	 * 印章停用列表
	 * 
	 * @param srh_name
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String srh_name) {

		List<Map<String, Object>> list = sealService.selectSealList(srh_name,
				page);

		ModelAndView mv = new ModelAndView("/jsp/seal/set/list");
		mv.addObject("list", list);
		return mv;
	}

	/**
	 * 印章添加
	 * 
	 * @return
	 */
	@RequestMapping("/add_form")
	public ModelAndView add_form() {

		List<t_seal_type> sealTypeList = sealTypeService.selectAllTypeList();
		t_bank bank = oc.findObjById(WebTools.getCurrentBankId(), t_bank.class);
		List<Map<String, Object>> userlist = sealService.selectSealUserList();

		ModelAndView mv = new ModelAndView("/jsp/seal/set/add_form");
		mv.addObject("sealTypeList", sealTypeList);
		mv.addObject("userlist", userlist);
		mv.addObject("bank", bank);
		return mv;
	}

	/**
	 * 印章添加
	 * 
	 * @param seal
	 *            印章信息
	 * @param seal_new
	 *            新章信息
	 * @param snote
	 *            备注
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(t_seal seal, t_seal_new seal_new, String snote) {

		boolean islive = sealNewService.checkUniqueCode(seal_new.get_id(),
				seal_new.getTrue_code());
		if (!islive) {
			super.setTipMsg("该编码已存在，请重新输入！", Tip.Type.error);
			ModelAndView mv = new ModelAndView("/jsp/seal/set/add_form");
			return mv;
		}

		seal_new.setUser_id(WebTools.getCurrentUserId());
		seal_new.setBank_id(WebTools.getCurrentBankId());
		seal_new.setUser_bank_id(WebTools.getCurrentBankId());
		oc.saveObj(seal_new);

		seal.setSeal_new_id(seal_new.get_id());
		seal.setBank_id(WebTools.getCurrentBankId());
		seal.setUser_id(WebTools.getCurrentUserId());
		seal.setUse_bank_id(WebTools.getCurrentBankId());
		seal.setFlag("停用");
		seal.setFlag_stop("暂时停用");
		seal.setNote(snote);
		oc.saveObj(seal);

		logT.logH("新建印章", t_seal.class, seal.get_id());

		super.setTipMsg("保存成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 印章修改详细信息
	 * 
	 * @param seal_id
	 *            印章id
	 * @return
	 */
	@RequestMapping("/edit_form")
	public ModelAndView edit_form(String seal_id) {

		List<Map<String, Object>> userlist = sealService.selectSealUserList();
		List<t_seal_type> sealTypeList = sealTypeService.selectAllTypeList();
		t_seal seal = oc.findObjById(seal_id, t_seal.class);
		t_seal_new seal_new = oc.findObjById(seal.getSeal_new_id(),
				t_seal_new.class);
		t_bank bank = oc.findObjById(WebTools.getCurrentBankId(), t_bank.class);

		ModelAndView mv = new ModelAndView("/jsp/seal/set/edit_form");
		if (ChkTools.isNotNull(seal_new)) {
			t_seal_type_config config = oc
					.findObjById(seal_new.getSeal_type_config_id(),
							t_seal_type_config.class);
			mv.addObject("config", config);
		}
		mv.addObject("seal", seal);
		mv.addObject("bank", bank);
		mv.addObject("seal_new", seal_new);
		mv.addObject("userlist", userlist);
		mv.addObject("sealTypeList", sealTypeList);
		return mv;
	}

	/**
	 * 印章修改
	 * 
	 * @param seal_id
	 *            印章id
	 * @param seal
	 *            印章信息
	 * @param seal_new
	 *            新章信息
	 * @param snote
	 *            备注
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(String seal_id, t_seal seal, t_seal_new seal_new,
			String snote) {

		t_seal sealDb = oc.findObjById(seal_id, t_seal.class);

		boolean islive = sealNewService.checkUniqueCode(
				sealDb.getSeal_new_id(), seal_new.getTrue_code());
		if (!islive) {
			super.setTipMsg("该编码已存在，请重新输入！", Tip.Type.error);
			ModelAndView mv = new ModelAndView(
					"redirect:edit_form.htm?seal_id=" + seal_id);
			return mv;
		}

		sealDb.setName(seal.getName());
		sealDb.setFile_id(seal.getFile_id());
		sealDb.setNote(snote);
		sealDb.setSave_user_id(seal.getSave_user_id());
		sealDb.setTrue_code(seal.getTrue_code());
		oc.updateObj(sealDb);

		t_seal_new seal_newDb = oc.findObjById(sealDb.getSeal_new_id(),
				t_seal_new.class);
		seal_newDb.setDesc_s(seal_new.getDesc_s());
		seal_newDb.setSeal_type_config_id(seal_new.getSeal_type_config_id());
		seal_newDb.setSeal_type_id(seal_new.getSeal_type_id());
		seal_newDb.setTitle(seal_new.getTitle());
		seal_newDb.setUser_id(WebTools.getCurrentUserId());
		oc.updateObj(seal_newDb);
		logT.logH("修改印章", t_seal.class, sealDb.get_id());
		super.setTipMsg("修改成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 印章删除
	 * 
	 * @param seal_id
	 *            印章id
	 * @return
	 */
	@RequestMapping("/del")
	public ModelAndView del(String seal_id) {
		t_seal seal = oc.findObjById(seal_id, t_seal.class);
		System.err.println(seal.getFlag() + "=============="
				+ seal.getFlag_stop());
		if (!"停用".equals(seal.getFlag()) || !"暂时停用".equals(seal.getFlag_stop())) {
			super.setTipMsg("该印章还在使用中！", Tip.Type.warning);
			ModelAndView mv = new ModelAndView("redirect:list.htm");
			return mv;
		}
		oc.delObjById(seal_id, t_seal.class);
		oc.delObjById(seal.getSeal_new_id(), t_seal_new.class);

		logT.logH("删除印章", t_seal.class, seal.get_id());

		super.setTipMsg("删除成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

}
